import { csv } from "./csv"
import { logMgr } from "../Manager/LogMgr"

class ExcelMgr {
    public static instance: ExcelMgr = null
    public static getInstance() {
        if (!ExcelMgr.instance) {
            ExcelMgr.instance = new ExcelMgr()
        }
        return ExcelMgr.instance
    }

    private _bLoad = false

    async loadAll() {
        return new Promise((resolve, reject) => {
            if (this._bLoad) {
                resolve(true)
                return
            }
            this._bLoad = true
            let allFiles = csv.allFiles
            let count = csv.allFiles.length
            for (let i = 0; i < allFiles.length; i++) {
                cc.resources.load(`Config/${allFiles[i]}`, cc.JsonAsset, (err, res) => {
                    count--
                    if (!err) {
                        csv.loadConfig(allFiles[i], res)
                        logMgr.log(`config ${allFiles[i]}`)
                    } else {
                        logMgr.error(`load config ${allFiles[i]} fail`)
                    }
                    if (count <= 0) resolve(true)
                })
            }
        })

    }
}

export let excelMgr = ExcelMgr.getInstance()
